package com.shop.service.user.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.service.module.entity.Result;
import com.shop.service.module.entity.UserEntity;
import com.shop.service.module.service.ShopUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RequestMapping("/shop")
@Slf4j
@RestController
public class ShopUserController {
    @Autowired
    protected ShopUserService shopUserService;


    @GetMapping("/list/page")
    public Result getListForPage(
            @RequestParam(value = "pno", defaultValue = "1") Integer pno,
            @RequestParam(value = "psize", defaultValue = "10") Integer psize,
            @RequestParam(value = "username", defaultValue = "") String username,
            @RequestParam(value = "phone", defaultValue = "") String phone,
            @RequestParam(value = "beginTime", defaultValue = "") String beginTime,
            @RequestParam(value = "endTime", defaultValue = "") String endTime,
            @RequestParam(required = false) Integer freeze
    ) throws ParseException {

        return shopUserService.getListForPage(pno, psize, username, freeze, phone, beginTime, endTime);
    }

    @PutMapping("insert")
    public Result insert(@RequestBody UserEntity userEntity) {

        return shopUserService.insert(userEntity);
    }

    @GetMapping("find/id/{id}")
    public Result getShopUserById(@PathVariable("id") Integer id) {
        UserEntity userEntity = shopUserService.getById(id);
        return Result.end(200, userEntity, "查询成功");
    }

    @PutMapping("update")
    public Result update(@RequestBody UserEntity userEntity) {
        boolean result = shopUserService.updateById(userEntity);
        return result ? Result.end(200, "", "修改成功") : Result.end(500, "", "修改失败");

    }

    @DeleteMapping("delete/id/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        boolean result = shopUserService.removeById(id);
        return result ? Result.end(200, "", "删除成功") : Result.end(500, "", "删除失败");
    }

    @GetMapping("set/freeze")
    public Result setFreeze(@RequestParam("id") Integer id,
                            @RequestParam("freeze") Integer freeze) {
        //按照id查找用户
        UserEntity userEntity = shopUserService.getById(id);
        //设置冻结状态
        userEntity.setFreeze(freeze);
        boolean result = shopUserService.updateById(userEntity);
        return result ? Result.end(200, "", "修改成功") : Result.end(500, "", "修改失败");
    }

    @PostMapping("login/password")
    public Result userLogin(@RequestBody UserEntity userEntity) {
        return shopUserService.loginPassword(userEntity.getUsername(), userEntity.getPassword());
    }

    @PostMapping("register")
    public Result register(@RequestBody UserEntity userEntity) {
        return shopUserService.register(userEntity.getUsername(), userEntity.getPassword());
    }
}
